<?php
function have_id($id){
	$master_lesson_ids=$_POST['master_lesson_ids'];
	if($master_lesson_ids!=''){
		foreach($master_lesson_ids as $master_lesson_id){
			if($master_lesson_id==$id) return true;
		}
	}
	else{
		return false;
	}
}
if($_SESSION['group_id']!=2){redir("./");exit;}

$lesson=mysql_fetch_assoc(mysql_query("select * from lesson where lesson_id='$_GET[lesson_id]'"));

$res=mysql_query("select * from course where course_id='$lesson[course_id]' and m_id='$_SESSION[m_id]'");

if(mysql_num_rows($res)==0){redir('./');exit;}
$course=mysql_fetch_assoc($res);

if($_GET['op']==''){
	if($_POST['op']=='update'){
		$master_lesson_ids=$_POST['master_lesson_ids'];
		if($master_lesson_ids!=''){
			$sum_amount=0;
			foreach($master_lesson_ids as $master_lesson_id){
				$qs_amount=$_POST["qs_amount$master_lesson_id"];
				$sum_amount+=$qs_amount;
				$master_lesson=mysql_fetch_assoc(mysql_query("select * from lesson where lesson_id='$master_lesson_id'"));
				if($qs_amount<1){
					$err="$master_lesson[lesson_name] จำนวนข้อไม่ถูกต้อง";
					break;//ออกจากลูป
				}
				else if($master_lesson['question_num']<$qs_amount){
					$err="$master_lesson[lesson_name] มีจำนวนคำถามเพียง $master_lesson[question_num] ข้อ";
					break;//ออกจากลูป
				}
			}
			if(!$err){
				if($sum_amount!=$lesson['question_num'])$err="กรอกจำนวนคำถามให้รวมกันแล้วได้ $lesson[question_num] ข้อ";
				else{
					mysql_query("delete from question_special where lesson_id='$lesson[lesson_id]'");
					foreach($master_lesson_ids as $master_lesson_id){
						$qs_amount=$_POST["qs_amount$master_lesson_id"];
						//echo "$master_lesson_id,$qs_amount|";
						mysql_query("insert into question_special (lesson_id,master_lesson_id,qs_amount) values('$lesson[lesson_id]','$master_lesson_id','$qs_amount')");
					}
					redir("?mod=$mod&lesson_id=$_GET[lesson_id]",5);
					die("<div id=com_msg>ปรับปรุงข้อมูลเรียบร้อยแล้ว กรุณารอสักครู่ <img src=images/load.gif></div>");
				}
			}
		}else{
			$err="กรุณาเลือกบทเรียน และกรอกจำนวนคำถามให้รวมกันแล้วได้ $lesson[question_num] ข้อ";	
		}
	}
?>
<div class="box">
  <h3>ข้อสอบ : วิชา<?php echo $course['course_name'];?> | <?php echo $lesson['lesson_name'];?></h3>
  <div class="boxContent">
  <?php
  if($err!='')echo "<div id=error_msg>$err</div>";
  
  $sql="select * from lesson where course_id='$lesson[course_id]' and have_exam=1 and exam_type=0 order by lesson_name asc";
  $res=mysql_query($sql);
  if(mysql_num_rows($res)>0){
  ?>
    <form method="post" onsubmit="return confirm('ยืนยันการบันทึก?');">
	<table width="100%">
      <tr>
        <td width="50" align="center"><strong>ใช้งาน</strong></td>
        <td><strong>บทเรียน</strong></td>
        <td><strong>จำนวนข้อ</strong></td>
        </tr>
	  <?php
	  	$res=mysql_query($sql);
		$i=0;
	  	while($row=mysql_fetch_assoc($res)){
			++$i;
	  		$class=($i%2!=0)?'rowA':'rowB';
			$qs=mysql_fetch_assoc(mysql_query("select * from question_special where lesson_id='$lesson[lesson_id]' and master_lesson_id='$row[lesson_id]'"));
	  ?>
      <tr class="<?php echo $class;?>">
        <td width="50" align="center"><input type="checkbox" name="master_lesson_ids[]" value="<?php echo $row['lesson_id'];?>" <?php if($qs!='' || have_id($row['lesson_id']))echo "checked";?> /></td>
        <td><?php echo $row['lesson_name'];?></td>
        <td><input name="qs_amount<?php echo $row['lesson_id'];?>" value="<?php if($_POST["qs_amount$row[lesson_id]"]>0)echo $_POST["qs_amount$row[lesson_id]"];else echo $qs['qs_amount'];?>" /></td>
        </tr>
	  <?php
	  }
	  ?>
    </table>
    <div align="center">
    <input type="submit" class="button" value="บันทึก" />
    <input type="hidden" name="op" value="update" />
    </div>
	</form>
  <?php
  }else{
  	echo "<div id=error_msg>กรุณาเพิ่มบทเรียนที่มีข้อสอบก่อน</div>";
  }
  ?>
  </div>
</div>
<?php
}
?>